package graphdao

import (
	"power/core/dao/table"
	"power/core/localwrap"
	"power/core/service/common"

	"github.com/RocksonZeta/wrap/redised"
	"github.com/RocksonZeta/wrap/utils/sutil"
	"github.com/RocksonZeta/wrap/utils/timeutil"
)

const tableGraphField = "GraphField"
const tableGraphFieldId = "Id"

var logGraphFields = localwrap.GetLogger().Fork("power/core/dao/graphdao", "GraphFields")

//GraphFields 
type GraphFields struct {
	*redised.RedisedMysql
}

type GraphFieldsQueryParam struct {
	common.Page
	Name string
}

//Query 根据条件查询
func (s GraphFields) Query(param GraphFieldsQueryParam) ([]table.GraphField, int64) {
	logGraphFields.Debug().Func("Query").Interface("param", param).Send()
	wsql := " from GraphField where 1=1"
	if param.Name != "" {
		param.Name = "%" + param.Name + "%"
		wsql += " and name like :Name"
	}
	ssql := "select *" + wsql + " order by ct desc" + param.Limit()
	csql := "select count(*)" + wsql
	var r []table.GraphField
	s.Mysql.Select(&r, ssql, param)
	return r, s.Mysql.SelectInt(csql, param)
}

func (s GraphFields) Get(id int) table.GraphField {
	logGraphFields.Debug().Func("Get").Int("id", id).Send()
	var r table.GraphField
	s.RedisedMysql.Get(&r, tableGraphField, tableGraphFieldId, id)
	return r
}

func (s GraphFields) List(ids []int) []table.GraphField {
	logGraphFields.Debug().Func("List").Ints("ids", ids).Send()
	var r []table.GraphField
	s.RedisedMysql.List(&r, tableGraphField, tableGraphFieldId, ids)
	return r
}
func (s GraphFields) ListAsMap(ids []int) map[int]table.GraphField {
	logGraphFields.Debug().Func("ListAsMap").Ints("ids", ids).Send()
	l := s.List(ids)
	r := make(map[int]table.GraphField, len(l))
	for _, v := range l {
		r[v.Id] = v
	}
	return r
}

func (s GraphFields) Delete(id int) {
	logGraphFields.Debug().Func("Delete").Int("id", id).Send()
	old := s.Get(id)
	s.Mysql.Delete(tableGraphField, tableGraphFieldId, id)
	s.ClearCache(old)
}

func (s GraphFields) ClearCache(old table.GraphField) {
	logGraphFields.Debug().Func("ClearCache").Int("id", old.Id).Send()
	s.Redis.Del(s.KeyFn(tableGraphField, old.Id))
	s.Redis.Del(s.KVFn(tableGraphField, map[string]interface{}{"graphId": old.GraphId, "protoId": old.ProtoId, "fieldId": old.FieldId}))
	s.Redis.Del(s.KVFn(tableGraphField, map[string]interface{}{"protoId": old.ProtoId}))
	s.Redis.Del(s.KVFn(tableGraphField, map[string]interface{}{"fieldId": old.FieldId}))
}
func (s GraphFields) GetByGraphIdProtoIdFieldId(graphId int,protoId int,fieldId int) table.GraphField {
	logGraphFields.Debug().Func("GetByGraphIdProtoIdFieldId").Int("graphId", graphId).Int("protoId", protoId).Int("fieldId", fieldId).Send()
	var r table.GraphField
	s.RedisedMysql.GetByKvs(&r, tableGraphField, tableGraphFieldId, map[string]interface{}{"graphId": graphId, "protoId": protoId, "fieldId": fieldId})
	return r
}
func (s GraphFields) ListByProtoId(protoId int) []table.GraphField {
	logGraphFields.Debug().Func("ListByProtoId").Int("protoId", protoId).Send()
	var r []table.GraphField
	s.RedisedMysql.ListByKvs(&r, tableGraphField, tableGraphFieldId, map[string]interface{}{"protoId": protoId})
	return r
}
func (s GraphFields) ListByFieldId(fieldId int) []table.GraphField {
	logGraphFields.Debug().Func("ListByFieldId").Int("fieldId", fieldId).Send()
	var r []table.GraphField
	s.RedisedMysql.ListByKvs(&r, tableGraphField, tableGraphFieldId, map[string]interface{}{"fieldId": fieldId})
	return r
}
type GraphFieldsUpdateParam struct {
	Id int
}

func (s GraphFields) Update(param GraphFieldsUpdateParam) {
	logGraphFields.Debug().Func("Update").Interface("param", param).Send()
	s.Mysql.Patch(tableGraphField, tableGraphFieldId, param)
	s.Redis.Del(s.KeyFn(tableGraphField, param.Id))
	// s.ClearCache(s.Get(param.Id)) if no index updated
}

type GraphFieldsAddParam struct {
	GraphId int
	ProtoId int
	FieldId int
}

func (s GraphFields) Add(param GraphFieldsAddParam) table.GraphField {
	logGraphFields.Debug().Func("Add").Interface("param", param).Send()
	var t table.GraphField
	sutil.Copy(param, &t)
	t.Ct = timeutil.Now()
	s.Mysql.AddTable(t)
	s.Mysql.Insert(&t)
	s.ClearCache(t)
	return t
}
